
package com.eworking.eim;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;

import com.eworking.eim.encrypt.EIMCryptoUtils;
import com.eworking.eim.group.EIMGroupManager;
import com.eworking.eim.user.EIMUserManager;
import com.eworking.eim.util.EIMLog;
import com.eworking.eim.xmpp.InitSmackStaticCode;

public class EIMManager {
    
    /** 日志输出标签 */
    private static final String _TAG = EIMManager.class.getSimpleName();
    
    /** 是否自动登陆 */
    private boolean isAutoLogin = false;
    
    private static EIMManager manager = null;
    
    /** 聊天室管理 */
    protected EIMChatManager chatManager = null;
    
    /** 会话管理 */
    private EIMSessionManager sessionManager;
    
    /** 用户、好友管理 */
    protected EIMUserManager userManager = null;
    
    /**群组管理*/
    protected EIMGroupManager groupManager = null;
    
    /** 是否已经初始化 - 用于加载离线消息 */
    public boolean isAppInited = false;
    /**上下文对象*/
    private Context context;
    
    public static EIMManager getInstance() {
        if (manager == null) {
        	manager =new EIMManager();
        }
        return manager;
    }
    
    /**
     * 加载离线消息。
     */
    public void loadOfflineMessages() {
        this.isAppInited = true;
        try {
            // TODO 加载离线消息、好友申请信息
        } catch (Exception e) {
            EIMLog.e(_TAG, "加载离线消息失败," + e.getMessage());
        }
    }
    
    /**
     * 初始化
     * 
     * @param context
     */
    public void init(Context context) {
        Context applicationContext = context.getApplicationContext();
        // 加载配置信息
        boolean isLoadConfig = EIMConfig.getInstance().loadConfig(applicationContext);
        if (!isLoadConfig) {
            EIMLog.d(_TAG, "加载配置信息失败");
        }
        // 加载Smack相关类;对应SmackAndroid类中的功能
        InitSmackStaticCode.initStaticCode(applicationContext);
        this.context = applicationContext;
        this.chatManager = EIMChatManager.getInstance();
        this.sessionManager = EIMSessionManager.getInstance(applicationContext);
        this.userManager = EIMUserManager.getInstance();
        this.groupManager = EIMGroupManager.getInstance();
        // 发送设备信息到服务端
        DeviceCollector.sendDeviceInfo(applicationContext);
        // 设置消息传输的加密方式
        EIMCryptoUtils.getInstance().init(EIMCryptoUtils._ALGORIGHM_DES);
        // 自动登陆
        if (this.isAutoLogin) {
            String[] lastLoginUser = this.sessionManager.getLastLoginUser();
            if (!TextUtils.isEmpty(lastLoginUser[0]) && !TextUtils.isEmpty(lastLoginUser[1])) {
                // 登陆IM服务器
                this.sessionManager.login(lastLoginUser[0], lastLoginUser[1], null);
            }
        }
    }
    
    public String getVersion() {
		try {
			PackageManager manager = context.getPackageManager();
	    	 PackageInfo info  = manager.getPackageInfo(context.getPackageName(), 0);
			return info.versionName;
		} catch (NameNotFoundException e) {
			EIMLog.e(_TAG, "获取应用版本发生异常",e);
		}
        return "";
    }
    
    /**
     * 设置是否自动登陆
     * 
     * @param isAutoLogin
     */
    public void setAutoLogin(boolean isAutoLogin) {
        this.isAutoLogin = isAutoLogin;
    }
    
    public void setDebugMode(boolean debugger){
    	EIMLog.debugMode = debugger;
    }
}
